<?php
namespace app\admin\controller;
use app\common\model;
use app\common\model\GoodsCard;
class Upload extends Base
{   
    public $goodsModel;
    public function __construct () {
        parent::__construct();
    }
    /* 通用上传方法 */
    public function index () {
        if(request()->isPost()){
            if(input('type')) {
                $ret = model('qiniu')->upload(input('type'));
            }else{
                $ret = model('qiniu')->upload();
            }
            if(!$ret) {
                return json(["code"=>1,"message"=>"上传失败"]);
            }
            return json(["code"=>0,"message"=>"上传完成","data"=>[
                'path' => $ret['key'],
                'src' => tomedia($ret['key'])
            ]]);
        }
    }

    /* 秘钥文件上传方法 */
    public function cert () {
        if(request()->isPost()){
            $file = request()->file('file');
            if($file){
                $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
                if($ext!=='pem') {
                    return returnJson(1,'格式错误');
                }
                $info = $file->move(ROOT_PATH . 'cert');
                if($info){
                    return returnJson(0,'',ROOT_PATH . 'cert'.DS.$info->getSaveName());
                }else{
                    echo $file->getError();
                }
            }
        }
    }

    /* 提货券文件上传方法 */
    public function card () {
        if(request()->isPost()){
            $file = request()->file('file');
            if($file){
                $info = $file->move(ROOT_PATH . 'coach');
                if($info){
                    $res = GoodsCard::addCard(ROOT_PATH . 'coach'.DS.$info->getSaveName());
                    return returnJson(0,'','提交成功');
                }else{
                    echo $file->getError();
                }
            }
        }
    }

    /* 上传方法 */
    public function coach () {
        if(request()->isPost()){
            $file = request()->file('file');
            if($file){
                $info = $file->move(ROOT_PATH .DS. 'public'.DS.'upload'.DS.'coach');
                if($info){
                    return returnJson(0,'', request()->domain().DS.'upload'.DS.'coach'.DS.$info->getSaveName());
                }else{
                    echo $file->getError();
                }
            }
        }
    }
}
